/*
	DS Kanji Flash
	Copyright (C) 2009,2010 Bernhard Schelling

	This file is part of DS Kanji Flash.

	DS Kanji Flash is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	DS Kanji Flash is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with DS Kanji Flash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "global.h"
#include "dict.h"

static const char* pDict = NULL;
static int iKanjiTrain, iDict, iChar, iIdleFrames;
static const u8 *pVariants = NULL;
static sRect rbtnBack(20, 12, 216, 8);
static UL_IMAGE *imgKanji = NULL;

class cSceneTrain : public cScene
{
public:
	cSceneTrain() : cScene(SCN_TRAIN)
		{ WantsEveryFrame = true; }
	
	void NextChar()
	{
		iChar++;
		const char *pChar = pDict;
		for (int i = 0; i < iChar; i++) GetUCS(&pChar);
		if (*pChar != ' ') pVariants = GetCharStrokes(GetUCS(pChar), true);
		else { NextDict(); return; }
		DrawClearImage();

		wchar_t wcChar = GetUCS(pChar); 
		int iKanji = GetKanjiIndex(wcChar);
		if (imgKanji) { ulDeleteImage(imgKanji); imgKanji = NULL; }
		if (iKanji>=0) imgKanji = ulLoadImageFilePNG((char*)kanjipool_png[iKanji], (int)(kanjipool_png[iKanji+1]-kanjipool_png[iKanji]), UL_IN_VRAM, UL_PF_PAL4);
		else if (wcChar>=0x3000&&wcChar<=0x30FF) imgKanji = ulLoadImageFilePNG((char*)kanapool_png[wcChar-0x3000], (int)(kanapool_png[wcChar-0x3000+1]-kanapool_png[wcChar-0x3000]), UL_IN_VRAM, UL_PF_PAL4);
	}

	void NextDict()
	{
		pDict = GetDict(iKanjiTrain, ++iDict);
		if (pDict == NULL) pDict = GetDict(iKanjiTrain, (iDict = 0));
		iChar = -1;
		NextChar();
	}

	virtual void Enter(void *p1, void *p2, void *p3)
	{
		iKanjiTrain = (int)p1;
		iDict = (int)p2;
		iDict = -1;
		NextDict();
		DrawClearImage();
		iIdleFrames = 0;
	}

	virtual void ProcessInput()
	{
		if (ul_keys.pressed.touch && rbtnBack.touchButton()) cScene::GoTo(SCN_CARD);
		DrawProcessInput();
		if (ul_keys.held.touch) iIdleFrames = 0;
		else iIdleFrames++;
		if (iIdleFrames > 10 && GetDrawScoreVariants(pVariants) < 10) NextChar();
		if (iIdleFrames > 10) iIdleFrames = 0;
	}
	
	virtual void DrawScreen_Top()
	{
		ulDrawGradientRect(0, 0, 256, 192, RGB15(25, 25, 25), RGB15(25, 25, 25), RGB15(31, 31, 31), RGB15(31, 31, 31));

		if (!pDict) return;
		char chrCompound[64];
		const char *pChar = pDict; 
		int iCompoundLen = 0, cx = 27, cy = 16;

		sprintf(chrCompound, "%d. ", iDict+1);
		displayString(fntJapanese, chrCompound, 7, cy, COL_WHITE);

		iCompoundLen = 0;
		while (*pChar != ' ' && iCompoundLen<63) chrCompound[iCompoundLen++] = *(pChar++);
		chrCompound[iCompoundLen] = 0;
		displayString(fntJapanese, chrCompound, cx, cy, COL_WHITE);

		const char *pcw = pDict;
		for (int i = 0; i < iChar; i++) GetUCS(&pcw);
		int iWidthSoFar = widthString(fntJapanese, chrCompound, pcw-pDict);
		int iWidthCurChar = widthString(fntJapanese, pcw, 1);
		ulDrawFillRect(cx + iWidthSoFar, cy + 20, cx + iWidthSoFar + iWidthCurChar, cy + 22, COL_RED);
		cy += 27;

		iCompoundLen = 0;
		while (*(pChar++) != '[');
		while (*pChar != ']' && iCompoundLen<63) chrCompound[iCompoundLen++] = *(pChar++);
		chrCompound[iCompoundLen] = 0;
		displayString(fntJapanese, chrCompound, cx, cy, COL_YELLOW);
		cy += 29;

		ulDrawFillRect(25, cy - 2, 250, cy + 31, RGB15(15,15,15));
		bDrawFontShadows = false;
		int iText1 = 0, iText2 = 0, iText3 = 0;
		while (*(pChar++) != '/');
		while (*pChar != '\n')
		{
			for (iCompoundLen=0; *pChar!='/'&&*pChar!='\n'&&iCompoundLen<63; chrCompound[iCompoundLen++] = *(pChar++));
			chrCompound[iCompoundLen] = 0;
			if (iText1<=iText2&&iText1<=iText3) iText1 += 15+displayStringWidth(ul_lcdFont, chrCompound, cx+iText1, 01+cy, COL_WHITE);
			else if (iText2<=iText3)            iText2 += 15+displayStringWidth(ul_lcdFont, chrCompound, cx+iText2, 11+cy, COL_WHITE);
			else                                iText3 += 15+displayStringWidth(ul_lcdFont, chrCompound, cx+iText3, 21+cy, COL_WHITE);
			if (*pChar == '/') pChar++;
		}
		bDrawFontShadows = true;
		cy += 40;

		displayStringCenter(ul_lcdFont, "Draw:", 0, cy, 256);
		cy += 12;
		const char *pCharInfo = pDict;
		for (int i = 0; i < iChar; i++) GetUCS(&pCharInfo);
		ulDrawGradientRect(128-32-2, cy-2, 128+32+2, cy+64+2, RGB15(2,2,2), RGB15(2,2,2), RGB15(8,8,8), RGB15(8,8,8));
		if (imgKanji) ulDrawImageXY(imgKanji, 128-32, cy); 
		else { ulDrawFillRect(128-32, cy, 128+32, cy+64, COL_WHITE); char s[4]; s[SetUCS(s, GetUCS(pCharInfo))] = 0; ulScaleScreenView(inttof32(4), inttof32(4)); displayStringCenter(fntJapanese, s, 20/4, (cy+8)/4, 232/4); ulResetScreenView(); }

		//string str = "Draw: ";
		//PutUCS(str, GetUCS(pCharInfo));
		//displayStringCenter(fntJapanese, str.c_str(), 0, 160, 256);
		//str = ""; str += ", iChar: " + itos(iChar); str += ", iDict: " + itos(iDict);
		//displayStringCenter(fntJapanese, str.c_str(), 0, 175, 256);

	}
	
	virtual void DrawScreen_Bottom()
	{
		ulDrawGradientRect(0, 0, 256, 192, RGB15(31, 31, 31), RGB15(31, 31, 31), RGB15(25, 25, 25), RGB15(25, 25, 25));

		DrawDisplay(pVariants+1);
		
		drawButtonBG(rbtnBack, BUTTON_BG_BLUE, "Back");

		//uu8 correction[DRAW_MAX_STROKES][JUDGE_PREC][2];
		//ufloat fScore = GetDrawScore(pVariants+1, correction);
		//ufor (u8 s = 0; s < pVariants[1] && *(u32*)(void*)(correction[s]) != (u32)0xFFFFFFFF; s++)
		//u	for (int i = 0; i < JUDGE_PREC; i++)
		//u		ulDrawFillRect(correction[s][i][0]-2, correction[s][i][1]-2, correction[s][i][0]+1, correction[s][i][1]+1, COL_RED);
		//u	//ulDrawLine(mydpoints[s][i][0], mydpoints[s][i][1], refx, refy, COL_RED);
			
		//static wchar_t wcGuess = 0;
		//if (ul_keys.pressed.touch && rbtnBack.touchButton()) wcGuess = GuessChar();
		//char bla[100];
		//sprintf(bla, "sc:%f , num:%d", fScore, pVariants[1]);
		//sprintf(bla, "d:%d, s:%d, pres:%d", cDrawn, pStrokes[0], bDrawing);
		//drawButtonBG(rbtnBack, BUTTON_BG_BLUE, bla);
		//char pcGuess[4] = "";
		//if (wcGuess) pcGuess[SetUCS(pcGuess, wcGuess)] = 0;
		//displayStringCenter(fntJapanese, pcGuess, rbtnBack);
	}
	
} SceneTrain;
